#include "CollectQuest.h"

CollectQuest::CollectQuest(void){
	numOfItemsCollected = 0;
	questMaxNum = 0;

	questStatus = ONGOING;
}

CollectQuest::~CollectQuest(void){
	for(vector<Object *>::iterator itr = CrateList.begin(); itr != CrateList.end(); ++itr){
		delete (*itr);
	}
	CrateList.clear();
}

void CollectQuest::CreateCrate(Vector3D position, Vector3D oOff){
	Object *newCrate = new Object;
	newCrate->InitSprite(1);
	newCrate->SetSprite(0, "images/Crate.tga");
	newCrate->Init(position, 50, 50);
	newCrate->SetLife(5.0f);
	//newCrate->SetOnGround(true);
	newCrate->oOff = oOff;
	
	questMaxNum++;

	CrateList.push_back(newCrate);
}

void CollectQuest::CreatePickUp(Vector3D position, Item::no itemNumber){
	PickUp *newPickUp = new PickUp;
	newPickUp->InitPickUp( position, itemNumber );
	newPickUp->SetOnGround(true);
	(*PickUpList).push_back( newPickUp );
}

void CollectQuest::InitQuest(Character *playerInfo, vector<Platform *> *FloorList, vector<Platform *> *SpawnerList, vector<PickUp *> *PickUpList, int questTimeSeconds){
	BaseQuest::InitQuest(playerInfo, "Collect special items only dropped from enemies", questTimeSeconds);
	
	//this->EnemyList = EnemyList;
	this->FloorList = FloorList;

	this->PickUpList = PickUpList;

	//this->questMaxNum = NumOfItemsToCollect;

	for(vector<Platform *>::iterator itr = (*SpawnerList).begin(); itr != (*SpawnerList).end(); ++itr){
		this->CreateCrate((*itr)->GetPosition(), (*itr)->GetoOff());
	}
}

void CollectQuest::RenderQuest(void){
	for(vector<Object *>::iterator itr = CrateList.begin(); itr != CrateList.end(); ++itr){
		(*itr)->RenderObject();
	}

	BaseQuest::RenderQuest();

	glPushMatrix();
		glScalef(0.4f, 0.4f, 1.0f);
		freetype::print(fontStyle, 300, 530, "%d/%d Collected", numOfItemsCollected, questMaxNum);
	glPopMatrix();
}

bool CollectQuest::UpdateQuest(Vector3D offset, float elapsed){
	for(vector<Object *>::iterator itr = CrateList.begin(); itr != CrateList.end(); ++itr){
		if(playerInfo->Get_Character_Weapon()->GetBoundingBox(Object::FULLBODY).HitTest((*itr)->GetBoundingBox(Object::FULLBODY)) 
			&& playerInfo->Get_Character_Weapon()->CheckIfAttacking()){
			(*itr)->ReduceLife(playerInfo->Get_Character_Weapon()->GetAttackDamageAttack());
		}

		if((*itr)->GetLife() <= 0.0f){
			this->CreatePickUp((*itr)->GetPosition() + (*itr)->oOff, Item::no::APPLE);

			delete (*itr);
			CrateList.erase(itr);

			break;
		}

		(*itr)->UpdateObject(offset, elapsed);

		if(!(*itr)->GetOnGround()){
			for(vector<Platform *>::iterator fItr = (*FloorList).begin(); fItr != (*FloorList).end(); ++fItr){
				if((*fItr)->CollisionWithPlatform((*itr)->GetBoundingBox(Object::LEG)) == Platform::FLOOR){
					(*itr)->SetOnGround(true);
					
					break;
				}else{
					(*itr)->SetOnGround(false);
				}
			}
		}
	}

	for(vector<PickUp *>::iterator itr = (*PickUpList).begin(); itr != (*PickUpList).end(); ++itr){
		if((*itr)->GetBoundingBox(Object::COLLISIONTYPE::FULLBODY).HitTest(playerInfo->GetBoundingBox(Object::COLLISIONTYPE::FULLBODY))){
			if((*itr)->GetItemType() == Item::type::QUEST){
				numOfItemsCollected++;

				delete (*itr);
				(*PickUpList).erase(itr);

				break;
			}
		}
	}

	if(playerInfo->Check_Lives_Dead() || questTimer.CheckIfTimerEnd()){
		this->questStatus = BaseQuest::FAIL;
	}else if(numOfItemsCollected >= questMaxNum){
		this->questStatus = BaseQuest::COMPLETE;
	}

	return BaseQuest::UpdateQuest(offset, elapsed);
}